//==================================================================
//  Copyright (C) 2006-2007  Davide Pasca
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//==================================================================
///
///
///
///
//==================================================================

#include "stdafx.h"
#include "gxy_base.h"
#include "gxy_font.h"

//==================================================================
namespace GXY {

//==================================================================
Context	*_ctxp=NULL;

//==================================================================
#define MAX_STATE_STACK	16

static SystemState	_state_stack[MAX_STATE_STACK];
static int			_state_stack_idx;

//==================================================================
void PushState()
{
	if NOT( _state_stack_idx < MAX_STATE_STACK )
	{
		PASSERT( 0 );
		return;
	}
	_state_stack[ _state_stack_idx++ ] = _ctxp->_sys_state;
}

//==================================================================
void PopState()
{
	if NOT( _state_stack_idx > 0 )
	{
		PASSERT( 0 );
		return;
	}
	_ctxp->_sys_state = _state_stack[ --_state_stack_idx ];
}

//==================================================================
void SelectContext( Context *contextp )
{
	_ctxp = contextp;
}

//==================================================================
Context *CreateContext( RendDevice *rdevicep, int disp_w, int disp_h, float disp_aspect )
{
	Context	*ctxp = new Context( rdevicep, disp_w, disp_h, disp_aspect );

	ctxp->_sys_state.SetFontScale( 1.0f, 1.0f );
	ctxp->_sys_state.SetViewport( 0, 0, disp_w, disp_h );

/*
	#define DISPLAY_W	1280	//640
	#define DISPLAY_H	720		//480

	//SetViewport( 0, 0, GetDisplayWidth(), GetDisplayHeight() );
	SetFontScale( (float)DISPLAY_W/GetDisplayWidth(),
						(float)DISPLAY_H/GetDisplayHeight() );
	SetViewport( 0, 0, DISPLAY_W, DISPLAY_H );
*/

	return ctxp;
}

//============================================================================
RendDevice *GetRenderDevice()
{
	return _ctxp->_rdevicep;
}


//============================================================================
int GetDisplayWidth()
{
	return _ctxp->_sys_state._disp_w;
}

//============================================================================
int GetDisplayHeight()
{
	return _ctxp->_sys_state._disp_h;
}

//============================================================================
float GetDisplayAspect()
{
	return _ctxp->_sys_state._disp_aspect;
}

//============================================================================
void SetViewport( float x, float y, float w, float h )
{
	_ctxp->_sys_state.SetViewport( x, y, w, h );
}

//============================================================================
void GetViewport( float xywh[4])
{
	xywh[0] = _ctxp->_sys_state._viewport[0];
	xywh[1] = _ctxp->_sys_state._viewport[1];
	xywh[2] = _ctxp->_sys_state._viewport[2];
	xywh[3] = _ctxp->_sys_state._viewport[3];
}

//============================================================================
void SetGlobalColorModulation( float r, float g, float b, float a )
{
	_ctxp->_sys_state._global_alpha_coe = (int)(a * 256);
}

//==================================================================
};
